/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Color;

/**
 *
 * @author minaev
 */
public class FracFrame {

    private static class FracPanel extends javax.swing.JPanel {

        private final int depth = 6;
        private final int count_leafs = 10;
        private final float inc_ang = 0.3f;
        private final int start_size = 100;
        private final float coef_damp = 20f;

        @Override
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            drawFrac(g, 384, 384, 0, 0);
        }

        private void drawFrac(java.awt.Graphics g, int x, int y, float ang, int depth) {
            if (depth < this.depth) {
                int nx = x;
                int ny = y;
                int dx, dy;
                int dir = depth % 2 == 0 ? 1 : -1;
                float nang;
                for (int i = 0; i < count_leafs; i++) {
                    g.setColor(new Color(depth * 255 / this.depth,
                            depth * 255 * (i + 1) / this.depth / count_leafs,
                            (this.depth - depth) * 255 / this.depth));
                    nang = ang + inc_ang * i * dir;
                    dx = getX(start_size * (count_leafs - i)
                            / ((depth + 1) * coef_damp), 0, nang);
                    dy = getY(start_size * (count_leafs - i)
                            / ((depth + 1) * coef_damp), 0, nang);
                    g.drawLine(nx, ny, nx + dx, ny + dy);
                    nx += dx;
                    ny += dy;
                    drawFrac(g, nx, ny, ang + nang - 1.57075f * dir, depth + 1);
                }
            }

        }

        private int getX(float dx, float dy, float ang) {
            return (int) Math.round((Math.cos(ang) * Math.sqrt(dx * dx + dy * dy)));
        }

        private int getY(float dx, float dy, float ang) {
            return (int) Math.round((Math.sin(ang) * Math.sqrt(dx * dx + dy * dy)));
        }
    }

    public static void main(String[] args) {
        FracPanel ff = new FracPanel();
        javax.swing.JFrame frm = new javax.swing.JFrame("Frac");
        frm.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        frm.setContentPane(ff);
        frm.setSize(1024, 768);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
